package com.talent.web.redis;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.connection.MessageListener;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;


@Component
public class RedisSubscriber {

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    @PostConstruct
    public void subscribe() {
        redisTemplate.getConnectionFactory().getConnection().subscribe(new MessageListener() {
            @Override
            public void onMessage(Message message, byte[] pattern) {
                String channel = new String(message.getChannel());
                Object payload = message.toString();
                System.out.println("Received: " + payload + " on channel: " + channel);
                // 处理接收到的消息
            }
        }, "demo".getBytes());
    }
}